package com.framework.utils.StarPos;

import com.alibaba.fastjson.JSONObject;
import com.framework.utils.MD5Util;
import com.framework.utils.MapObjUtils;
import com.framework.utils.StarPos.CustomerImgUploadBean.CustomerImgUploadBean;

import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * 上传图片
 */
public class CustomerImgUpload {

    //private final static String BaseUrl = "http://sandbox.starpos.com.cn/emercapp";
    private final static String BaseUrl="https://gateway.starpos.com.cn/emercapp";
    //private final static String orgNo = "518";
    private final static String orgNo="1467";

    //private final static String key = "9773BCF5BAC01078C9479E67919157B8";
    private final static String key="6928A794B9F3ACFF82E9C0FCE18C4449";

    public static String upload(CustomerImgUploadBean uploadBean,String imgFile){
        uploadBean.setOrgNo(orgNo);
        uploadBean.setServiceId("6060606");
        SortedMap<String ,Object> map=new TreeMap<>();

        StringBuffer sb = new StringBuffer();
        try {
            Map<String, Object> registMap = MapObjUtils.objToMap(uploadBean);
            map.putAll(registMap);

            String waitSign="";
            Set<String> keys = map.keySet();

            Iterator<String> it = keys.iterator();
            System.out.print("验签字段:  ");
            while (it.hasNext()){
                String keytemp=it.next();

                waitSign+=String.valueOf(map.get(keytemp));

                System.out.print(keytemp+",");

            }
            System.out.println();
            waitSign+=key;

            String signValue= MD5Util.MD5(waitSign);

            //创建连接
            URL url = new URL(BaseUrl);

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type","application/json; charset=GBK");
            connection.connect();

            //POST请求
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());


            map.put("signValue",signValue);
            map.put("imgFile",imgFile);

            String content;
            content= JSONObject.toJSONString(map);
            System.out.println(content);
            out.write(content.getBytes("GBK"));//这样可以处理中文乱码问题

            //  out.writeBytes(content);//这个中文会乱码
            out.flush();
            out.close();

            //读取响应
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(),"gbk"));
            String lines;

            while ((lines = reader.readLine()) != null) {
                lines = new String(lines.getBytes(), StandardCharsets.UTF_8);

                sb.append(lines);

                System.out.println(lines);
            }
            reader.close();
            // 断开连接
            connection.disconnect();



        } catch (ProtocolException e) {
            return sb.toString();
        } catch (MalformedURLException e) {
            return sb.toString();
        } catch (IOException e) {
            return sb.toString();
        } catch (IllegalAccessException e) {
            return sb.toString();
        }
        return sb.toString();

    }

    /*
     *  实现字节数组向十六进制的转换方法一
     */
    public static String byte2HexStr(byte[] b) {
        try {
            FileInputStream fis = new FileInputStream("D:/test.jpg"); //本地图片
            BufferedInputStream bis = new BufferedInputStream(fis);
            java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();

            byte[] buff = new byte[1024];
            int len = 0;
            while ((len = fis.read(buff)) != -1) {
                bos.write(buff, 0, len);
            }
            byte[] result = bos.toByteArray();
            //得到图片的字节数组
            System.out.println("++++" + byte2HexStr(result));
            String str = byte2HexStr(result);
        } catch (Exception e) {

        }
        StringBuilder  hs = new StringBuilder();
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
               // hs = hs + "0" + stmp;
                hs=hs.append("0").append(stmp);
            } else {
                //hs = hs + stmp;
                hs=hs.append(stmp);
            }
        }
        return hs.toString().toUpperCase();
    }


}
